package com.ls.demo.tcp.service.impl;

import com.ls.demo.bean.Command;
import com.ls.demo.tcp.namespace.Namespaces;
import com.ls.demo.tcp.service.CommandService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * 云服务注册，应答云询问,主要获取设备的UUID
 * @author liShan
 * @date 2019/9/3 23:31
 */
@Service("command_0E")
@Log
public class CommandIdentityImpl implements CommandService {

    private final Namespaces namespaces = Namespaces.getInstance();

    @Override
    public Command execute(ChannelHandlerContext ctx, Command command) {
        AttributeKey<Boolean> identity = AttributeKey.valueOf("identity");
        ctx.channel().attr(identity).set(true);
        String boxId = StringUtils.join(command.getDataList(), "");
        AttributeKey<UUID> id = AttributeKey.valueOf("id");
        namespaces.addBoxChannel(boxId, ctx.channel().attr(id).get().toString());
        log.info(String.format("收到的身份信息: %s", boxId));
        return null;
    }
}
